package com.example.hotelmanager.controller;

import com.example.hotelmanager.entity.BookMsg;
import com.example.hotelmanager.entity.CheckIn;
import com.example.hotelmanager.entity.Room;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.entity.vo.RoomMsg;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/27 22:24
 */
@RestController
@Slf4j
@CrossOrigin
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private BackgroundService backgroundService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CheckInService checkInService;

    /**
     * 查看房间信息顺便将房间使用情况一起返回
     * @param request
     * @return
     */
    @GetMapping("/getAllRooms")
    public Response getAllRooms(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){//用户状态正常后台或者是前台管理员
            List<Room> rooms = roomService.list();
            if(rooms != null && rooms.size()>0){
                Timestamp now = new Timestamp(System.currentTimeMillis());
                Timestamp later = new Timestamp(System.currentTimeMillis()+31536000000l);
                //获取当前的预定信息
                List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime(now,later);
                //获取当前正在入住的信息
                List<CheckIn> checkIns = checkInService.getValidCheckIns(now,now);
                List<RoomMsg> roomMsgs = new ArrayList<>();
                for(Room room:rooms){
                    RoomMsg roomMsg = new RoomMsg(room.getRoomId(),room.getSize(),room.getGrade(),room.getRent(),room.getEarnest(),room.getMaxNum(),room.getPosition(),-1,"");
                    int f = 0;
                    for(CheckIn checkIn:checkIns){
                        if(f == 1)
                            break;
                        if(checkIn.getRoomId().equals(roomMsg.getRoomId())){
                            roomMsg.setState(1);
                            Timestamp fromTime = checkIn.getFromTime();
                            Timestamp toTime = checkIn.getToTime();
                            String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
                            roomMsg.setTime(time);
                            roomMsgs.add(roomMsg);
                            f = 1;
                        }
                    }

                    for(BookMsg bookMsg:bookMsgs){
                        if(f == 1)
                            break;
                        if(bookMsg.getResultRoom().equals(roomMsg.getRoomId())){
                            roomMsg.setState(0);
                            Timestamp fromTime = bookMsg.getFromTime();
                            Timestamp toTime = bookMsg.getToTime();
                            String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
                            roomMsg.setTime(time);
                            roomMsgs.add(roomMsg);
                            f = 1;
                        }
                    }
                    if(f != 1)
                        roomMsgs.add(roomMsg);
                }
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("roomMsgs",roomMsgs);//将房间预定房间信息封装成map
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @GetMapping("/getNullRooms")
    public Response getNullRooms(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<Room> rooms = roomService.list();
            List<Room> nullRooms = roomService.getNullRooms();
            if(rooms != null && rooms.size()>0) {
                Map<String, Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("nullRooms", nullRooms);
                resultMap.put("token", token);
                return (new Response()).success(resultMap);

            }return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 多表联动新增房间信息要添加租金信息和定金信息
     * @param request
     * @param room
     * @return
     */
    @PostMapping("/addRoom")
    public Response addRoom(HttpServletRequest request, Room room){
        String num = (String) request.getAttribute("num");

        if(backgroundService.getById(num) != null){
            roomService.saveOrUpdate(room);
            roomService.addRoom(room);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @DeleteMapping("/deleteRoom")
    public Response deleteRoom(HttpServletRequest request,String id){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            roomService.deleteRoom(id);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    /**
     * 按等级获取指定时间内可以安排给预定客户或入住客户的房间查询等级内的空房
     * @param request
     * @return
     */
    @GetMapping("/getNullRoomsByRank")
    public Response getNullRoomsByRank(HttpServletRequest request,long fromTime,long toTime,String rank) {
        String num = (String) request.getAttribute("num");
        if (backgroundService.getById(num) != null || frontService.getById(num) != null) {
            List<Room> rooms = roomService.list();
            List<Room> nullRooms=roomService.getNullRoomsByRank(rooms,fromTime,toTime,rank);
            if (rooms != null && rooms.size() > 0) {
                Map<String, Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("nullRooms", nullRooms);
                resultMap.put("token", token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    } /**
     * 获取所有已经入住的房间
     * @param request
     * @return
     */
    @GetMapping("/getHasCheckedRoom")
    public Response getHasCheckedRoom(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
           List<RoomMsg> roomMsgs=roomService.getHasCheckedRoom();
            if(roomMsgs != null && roomMsgs.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("roomMsgs",roomMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    /**
     * 获取所有已经被预定的房间
     * @param request
     * @return
     */
    @GetMapping("/getHasBookedRoom")
    public Response getHasBookedRoom(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<RoomMsg> roomMsgs=roomService.getHasBookedRoom();
            if(roomMsgs != null && roomMsgs.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("roomMsgs",roomMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 封装成RoomMsg
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/getRoomById")
    public Response getRoomById(HttpServletRequest request,String id){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            Room room = roomService.getById(id);

            if(room != null ){
                RoomMsg roomMsg=roomService.getid(room);
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("roomMsg",roomMsg);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }


}
